﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AnalisisDeRiesgos.Dal;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

namespace AnalisisDeRiesgos.Models.Management
{
    public class CreateManagementModel
    {
        [DisplayName("Nombre de Jefatura/Gerencia *")]
        [Required(ErrorMessage = "El nombre de la Jefatura/Gerencia es requerida")]
        [StringLength(50, ErrorMessage = "El nombre debe ser máximo de 50 caracteres")]
        [RegularExpression(@"^[a-zA-Z0-9 \(\)ñÑ\-.,áéíóúÁÉÍÓÚ]+$", ErrorMessage = "El nombre debe ser solo letras o números")]
        public string Name { get; set; }
        
        [DisplayName("Jefatura/Gerencia Padre *")]
        [Required(ErrorMessage="Una jefatura/gerencia es necesaria")]
        public int Parent { get; set; }

        [DisplayName("Responsable *")]
        [Required(ErrorMessage="El usuario responsable es requerido")]
        public int Responsible { get; set; }
        
        public IEnumerable<SelectListItem> Managements
        {
            get
            {
                return managements;
            }
        }

        public IEnumerable<SelectListItem> Users
        {
            get
            {
                return users;
            }
        }

        private List<SelectListItem> managements;
        private List<SelectListItem> users;
        riskanalysisEntities entities = new riskanalysisEntities();

        public CreateManagementModel()
        {
            managements = new List<SelectListItem>();
            users = new List<SelectListItem>();
            FillLists();
        }

        private void FillLists()
        {
            int privilege = Convert.ToInt32(Privileges.AreaSecurity);
            var queryUser = from u in entities.users
                            where u.privilege <= privilege && u.enabled
                            select u;

            foreach (user item in queryUser)
            {
                users.Add(new SelectListItem
                    {
                        Text = item.firstName + " " + item.lastName + " " + item.secondLastName,
                        Value = item.CI.ToString()
                    });
            }

            var queryManagements = from m in entities.managements
                                   where m.enabled
                                   select m;

            foreach (management item in queryManagements)
            {
                managements.Add(new SelectListItem
                {
                    Text = item.name,
                    Value = item.managementId.ToString()
                });
            }
        }

        public void Save()
        {
            var queryManagements = from m in entities.managements
                                   where m.enabled && m.name.ToLower().Equals(Name.ToLower())
                                   select m;

            if (queryManagements.Count() >= 1)
                throw new RegisterManagementException("Ya existe una Gerencia/Jefatura con ese nombre");

            entities.AddTomanagements(
                new management
                {
                    name = Name,
                    parentManagement = Parent,
                    responsible = Responsible,
                    enabled = true
                });
            entities.SaveChanges();
        }
    }
}